//-------------------------------------------------------------------------------------------------
// <copyright file="ITable.cs" company="Oliver Hallam">
// Copyright (c) 2008 Oliver Hallam
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace DBLite
{
    using System;
    using System.Diagnostics.Contracts;

    /// <summary>
    /// Represents a table in the database.
    /// </summary>
    [ContractClass(typeof(ITableContract))]
    public interface ITable
    {
        #region Properties

        /// <summary>
        /// Gets the columns that make up this table.
        /// </summary>
        /// <value>The columns that make up this table.</value>
        Columns Columns
        {
            get;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds a row to the table.
        /// </summary>
        /// <returns>The ID of the added row.</returns>
        int CreateRow();

        /// <summary>
        /// Deletes a row from the table.
        /// </summary>
        /// <param name="rowId">The ID of the row to be deleted.</param>
        void DeleteRow(int rowId);

        /// <summary>
        /// Gets the value of a cell in the table.
        /// </summary>
        /// <param name="rowId">The ID of the row to retrieve.</param>
        /// <param name="columnId">The ID of the column to retrieve.</param>
        /// <returns>The value of the cell.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="rowId"/> refers to a row that is not in the table, or <paramref name="columnId"/> is not
        /// a valid column ID for this table.
        /// </exception>
        string GetCell(int rowId, int columnId);

        /// <summary>
        /// Returns a value indicating whether a row exists in the table with the given ID.
        /// </summary>
        /// <param name="rowId">The row ID.</param>
        /// <returns>
        /// <see langword="true" /> if a row with ID <paramref name="rowId"/> exists; 
        /// <see langword="false" /> otherwise.
        /// </returns>
        bool RowExists(int rowId);

        /// <summary>
        /// Sets the value of a cell in the table.
        /// </summary>
        /// <param name="rowId">The ID of the row to set.</param>
        /// <param name="columnId">The ID of the column to set.</param>
        /// <param name="value">The value to set the cell to.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="rowId"/> refers to a row that is not in the table, or <paramref name="columnId"/> is not
        /// a valid column ID for this table.
        /// </exception>
        void SetCell(int rowId, int columnId, string value);

        #endregion
    }

    /// <summary>
    /// Contracts for the <see cref="ITable"/> interface. 
    /// </summary>
    [ContractClassFor(typeof(ITable))]
    internal class ITableContract : ITable
    {
        /// <summary>
        /// Gets the columns that make up this table.
        /// </summary>
        /// <value>The columns that make up this table.</value>
        Columns ITable.Columns
        {
            get
            {
                Contract.Ensures(Contract.Result<Columns>() != null);

                return Contract.Result<Columns>();
            }
        }

        /// <summary>
        /// Adds a row to the table.
        /// </summary>
        /// <returns>The ID of the added row.</returns>
        int ITable.CreateRow()
        {
            Contract.Ensures(Contract.Result<int>() >= 0);

            return Contract.Result<int>();
        }

        /// <summary>
        /// Deletes a row from the table.
        /// </summary>
        /// <param name="rowId">The ID of the row to be deleted.</param>
        void ITable.DeleteRow(int rowId)
        {
        }

        /// <summary>
        /// Gets the value of a cell in the table.
        /// </summary>
        /// <param name="rowId">The ID of the row to retrieve.</param>
        /// <param name="columnId">The ID of the column to retrieve.</param>
        /// <returns>The value of the cell.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="rowId"/> refers to a row that is not in the table, or <paramref name="columnId"/> is not
        /// a valid column ID for this table.
        /// </exception>
        string ITable.GetCell(int rowId, int columnId)
        {
            return Contract.Result<string>();
        }

        /// <summary>
        /// Returns a value indicating whether a row exists in the table with the given ID.
        /// </summary>
        /// <param name="rowId">The row ID.</param>
        /// <returns>
        /// <see langword="true"/> if a row with ID <paramref name="rowId"/> exists;
        /// <see langword="false"/> otherwise.
        /// </returns>
        bool ITable.RowExists(int rowId)
        {
            return Contract.Result<bool>();
        }

        /// <summary>
        /// Sets the value of a cell in the table.
        /// </summary>
        /// <param name="rowId">The ID of the row to set.</param>
        /// <param name="columnId">The ID of the column to set.</param>
        /// <param name="value">The value to set the cell to.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="rowId"/> refers to a row that is not in the table, or <paramref name="columnId"/> is not
        /// a valid column ID for this table.
        /// </exception>
        void ITable.SetCell(int rowId, int columnId, string value)
        {
        }
    }
}